***************************************************
***************************************************
**********                               ********** 
**********        Macro Analysis	 	 ********** 
**********                               **********    
***************************************************
***************************************************


clear all
global beg_path `" XXX "' // Indicate your working directory

*Uploading the dataset
use `"${beg_path}Data\Macro\Macro_dataset.dta"', clear


***************************************************
**********                               ********** 
**********   Finalising the variables 	 ********** 
**********                               **********    
***************************************************


************************************
* Number of Electoral Institutions *
************************************

// Local level
gen local_l = Vdem_v2ellocelc > 0
// Regional level 
gen regional_l = Vdem_v2elsrgel > 0 
// EU level 
gen eu = year > 1978 & inlist(country, 8, 26, 34, 37, 51, 116, 49, 64, 78)
replace eu = 1 if year > 1980 & inlist(country, 39) // greece
replace eu = 1 if year > 1985 & inlist(country, 103, 90)
replace eu = 1 if year > 2003 & inlist(country, 24, 31, 45, 59, 63, 89, 98, 99, 23)
replace eu = 1 if year > 1994 & inlist(country, 106, 33, 5)
replace eu = 1 if year > 2006 & inlist(country, 13, 91)
replace eu = 1 if year > 2012 & inlist(country, 22)
// Bicameralism 
gen bicam = Vdem_v2lgbicam > 1
// Referendum 
egen ref_yes_or_no = rowtotal(Vdem_v2ddyrci_4 Vdem_v2ddyrci_3 Vdem_v2ddyrci_2 Vdem_v2ddyrci_1 ///
Vdem_v2ddyror_4 Vdem_v2ddyror_3 Vdem_v2ddyror_2 Vdem_v2ddyror_1 Vdem_v2ddyrrf_4 Vdem_v2ddyrrf_3 ///
Vdem_v2ddyrrf_2 Vdem_v2ddyrrf_1 Vdem_v2ddyrpl_4 Vdem_v2ddyrpl_3 Vdem_v2ddyrpl_2 Vdem_v2ddyrpl_1)
recode ref_yes_or_no (0=0) (1/max=1) (.=.), gen(referendum) 

*Generating the number of electoral institutions 
egen temp1 = rowtotal(referendum local_l regional_l eu bicam Vdem_v2ex_elechos)

gen N_el_instit  = temp1/6 // to make the variable vary between 0 and 1
lab var N_el_instit "Elective Institutions" 
drop temp1


*********************
* Control variables *
*********************

*Decades dummies
foreach i of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
local k = `i' + 10
gen decade_`i's = year >=`i' & year<`k' 
} 

*Label the dummies
foreach var of varlist decade_* {
local x = substr("`var'", -5, 5)
lab var `var' `x' 
}

*New democracies
gen beg_dem = year - PLT_dem0_time  // Time since democratisation 
gen new = PLT_dem0_time < 16 
gen postcommunist = inlist(country, 1, 7, 13, 22, 24, 25, 31, 45, 56, 57, 59, 63, 65, 72, 73, 74, 89, 91, 92, 94, 95, 98, 99, 115, 120)

*Distinguishing 3 types of new democracies (Pre-1974, Post-1974, and Postcommunist)
gen pre_1974_new = 0 
lab var pre_1974_new "Pre-1974 Democratization"
replace pre_1974_new = (PLT_dem0_time*-1) if PLT_dem0_time < 16 & beg_dem < 1974
gen postcommunist_new = 0
lab var postcommunist_new "Post-Communist Democratization" 
replace postcommunist_new = (PLT_dem0_time*-1) if PLT_dem0_time < 16 & postcommunist == 1
gen post_1974_new = 0
lab var post_1974_new "Post-1974 Democratization" 
replace post_1974_new = (PLT_dem0_time*-1) if PLT_dem0_time < 16 & beg_dem > 1973 & postcommunist==0 

*Generate additional variables 
gen year_sq = year*year // Year squared
gen log_gdp_pc = ln(gdp_pc) // logged gdp per capita
lab var log_gdp_pc "GDP p/c (log)" 
gen log_el_size = ln(electorate_size) // logged electorate size

*Transformation of FDI_flows following Marshall & Fisher
gen FDI_flows = .
gen temp_x = WB_v_513 + WB_v_515
replace FDI_flows = -ln(-temp_x+1) if temp_x<0
replace FDI_flows = ln(temp_x+1) if temp_x>=0
drop temp_x

gen FDI_inflows = . // FDI inflows
replace FDI_inflows = -ln(-WB_v_513+1) if WB_v_513<0
replace FDI_inflows = ln(WB_v_513+1) if WB_v_513>=0

gen FDI_outflows = . // FDI outflows
replace FDI_outflows = -ln(-WB_v_515+1) if WB_v_515<0
replace FDI_outflows = ln(WB_v_515+1) if WB_v_515>=0

lab var FDI_flows "FDI Flows" 
lab var FDI_inflows "FDI Inflows" 
lab var FDI_outflows "FDI Outflows" 

*Filter for countries that are consolidated democracies in the 1960s and 2000s (18 countries in Figure 4)
cap drop temp* filter
gen temp1 = decade_1960s ==1 & new !=1 & share_affluent!=. & Majority!=. & Closeness!=.  & Concurrent!=. & CV !=. & N_el_instit!=. & CV_enforced ==0 //
gen temp2 = decade_2000s ==1 & new !=1 & share_affluent!=. & Majority!=. & Closeness!=.  & Concurrent!=. & CV !=. & N_el_instit!=. & CV_enforced ==0  // 
egen temp3 = max(temp1), by(country)
egen temp4 = max(temp2), by(country)
gen filter = temp3 ==1 & temp4 ==1 
fre country if filter==1
drop temp*

*Creating election number (our time variable) 
sort country year EL_TYPE multiple_el_id
bysort country: gen el_num = _n



***********************************
**********               ********** 
**********    Analysis 	 ********** 
**********               **********    
***********************************

tsset country el_num

*Creating macros with control variables  
global decades "decade_1940s decade_1950s decade_1960s decade_1970s decade_1980s decade_1990s decade_2000s" 
global new "pre_1974_new post_1974_new postcommunist_new "
global institutions "Majority Closeness Concurrent CV " // 

cd `"${beg_path}Results"'

*** Table 1 in the Manuscript *** 

eststo clear
eststo M1 : xtreg Turnout $decades EL_TYPE $new, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M2 : xtreg Turnout $decades $new if EL_TYPE==0, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M3 : xtreg Turnout $decades $new if EL_TYPE==1, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M4 : xtreg Turnout $decades EL_TYPE  if CV_enforced ==1 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M5 : xtreg Turnout $decades EL_TYPE  if CV_enforced==0 & new==0 & share_affluent!=., fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M6 : xtreg Turnout $decades EL_TYPE if CV_enforced==0 & new==0 & share_affluent!=. & filter==1, fe cluster(country) 
estadd local fixed "Yes" , replace

* LaTeX 

esttab M1 M2 M3 M4 M5 M6 using Table_1.tex, incelldelimite("\:")   one stats(fixed N r2, labels("Country FE" "N" "R2 (within)") ///
layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 2)) replace b(2) se(2) wide staraux ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none)  order(decade* EL_TYPE pre_1974_new post_1974_new postcommunist_new) nodep /// 
	label /// 
	mtitles("\shortstack{\\A\\All\\elections}" "\shortstack{\\B\\All\\ Legislative elections}" ///
	"\shortstack{\\C\\All\\ Presidential elections}" "\shortstack{\\D\\Established dem.\\Enforced CV}" ///
	"\shortstack{\\E\\Established dem.\\No enforced CV}"  "\shortstack{\\F\\ Establishe dem.\\ 18 cases (1960-2010) }")  ///
	nonumbers nolegend note("") alignment(d{10})

*Word 

esttab M1 M2 M3 M4 M5 M6 using Table_1.rtf,   one stats(fixed N r2, labels("Country FE" "N" "R2 (within)") ///
 fmt(0 0 2)) replace b(2) se(2) wide staraux incelldelimiter(" ")  ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none)  order(decade* EL_TYPE pre_1974_new post_1974_new postcommunist_new) nodep /// 
	label /// 
	mtitles("A All elections" "B All Legislative elections" ///
	"C All Presidential elections" "D Established dem. Enforced CV" ///
	"E Established dem. No enforced CV"  "F Establishe dem. 18 cases (1960-2010)")  ///
	nonumbers nolegend note("")

	
	
*** Table 2 in the Manuscript *** 	

eststo clear
eststo M7: xtreg Turnout $decades $institutions OECD_TU_density EL_TYPE if CV_enforced == 0 & new==0 , fe cluster(country)
estadd local fixed "Yes" , replace // Note: the condition share_affluent!=. is not applied to maximise the number of observations, its application leads to the same substantive results
eststo M8 : xtreg Turnout $decades $institutions MAPP_party_members EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M9: xtreg Turnout $decades $institutions voting_age18_cum  EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) // Note: the condition share_affluent!=. is not applied to maximise the number of observations, its application leads to the same substantive results
estadd local fixed "Yes" , replace

*Testing Economic inequality
// First uploading data from Solt 2020
preserve
merge m:1 country year  using `"${beg_path}Data\Macro\swiid9_0_for_merge.dta"'
drop if _merge == 2
drop _merge  
lab var gini_disp "Gini (disp. income)"
// Calculating R2 (necessary because of the MI nature of the data))
mi query
local M = r(M)
scalar xr2 = 0
mi xeq 1/`M': reg  Turnout  $decades  $institutions gini_disp EL_TYPE if CV_enforced == 0 & new==0; scalar xr2 = xr2 + e(r2)
scalar xr2 = xr2/100
di as txt "R2 over imputed data = " as res xr2
// The actual analysis 
eststo M10: mi estimate,  post: xtreg Turnout $decades  $institutions  gini_disp  if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
estadd scalar r2 = xr2, replace
restore

eststo M11 : xtreg Turnout $decades $institutions FDI_flows EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) // Note: the condition share_affluent!=. is not applied to maximise the number of observations, its application leads to the same substantive results
estadd local fixed "Yes" , replace
eststo M12 : xtreg Turnout $decades $institutions Manif_rile EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) // Note: the condition share_affluent!=. is not applied to maximise the number of observations, its application leads to the same substantive results
estadd local fixed "Yes" , replace

* LaTeX

esttab M7 M8 M9 M10 M11 M12 using Table_2.tex, incelldelimite("\:")  one stats(fixed N r2, labels("Country FE" "N" "R2 (within)")  ///
layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 2)) replace b(2) se(2) wide staraux ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) order(decade* EL_TYPE Majority Closeness Concurrent CV OECD_TU_density_admin ///
		MAPP_party_members voting_age18_cum gini_disp FDI_flows Manif_rile)  ///
		nodep refcat(OECD_TU_density_admin "\textit{Group Mobilization:}" voting_age18_cum "\textit{Voting Age Reforms}" ///  
		gini_disp "\textit{Income Inequality:}" FDI_flows "\textit{Globalization:}", nolabel) substitute("0.00\:(.)" " ") ///   
		label  varlabels(_cons Constant gini_disp "Gini (disp. income)" CV_enforced "CV enforced (regions)", ///
		elist(CV \hline MAPP_party_members \hline voting_age18_cum \hline gini_disp \hline))  /// 
		mtitles("\shortstack{\\G\\Established dem.\\No enforced CV}" "\shortstack{\\H\\Established dem.\\No enforced CV}" ///
		"\shortstack{\\I\\Established dem.\\No enforced CV}" "\shortstack{\\J\\Established dem.\\No enforced CV}" ///
		"\shortstack{\\K\\Established dem.\\No enforced CV}"  "\shortstack{\\L\\ Established dem.\\No enforced CV}")  ///
		nonumbers nolegend note("") alignment(d{10})

* Word 
		
	esttab M7 M8 M9 M10 M11 M12 using Table_2.rtf, onecell stats(fixed N r2, labels("Country FE" "N" "R2 (within)")  ///
       fmt(0 0 2)) replace b(2) se(2) wide staraux incelldelimiter(" ")  ///
		star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) order(decade* EL_TYPE Majority Closeness Concurrent CV OECD_TU_density_admin ///
		MAPP_party_members voting_age18_cum gini_disp FDI_flows Manif_rile)  ///
		nodep  substitute("0.00 (.)" " ")     alignment(cr) ///   nobase 
		label mtitles("G" "H" "I" "J" "K" "L") nonumbers nolegend note("") 
		
		// The following information for mtitles has to be entered manually (too long for esttab)
		"G Established dem. No enforced CV}" "H Established dem. No enforced CV}"  
		"I Established dem. No enforced CV}" "J Established dem. No enforced CV}"  
		"K Established dem. No enforced CV}"  "L Established dem. No enforced CV}" 
		
	
		
		
		
*** Table 3 in the Manuscript *** 			
		
eststo clear
eststo M12 : xtreg Turnout $decades $institutions share_affluent EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M13 : xtreg Turnout $decades $institutions N_el_instit EL_TYPE if CV_enforced == 0 & new==0 & share_affluent!=., fe cluster(country)  // Note: the condition share_affluent!=. is applied to ensure comparability with other models in Table 3, its non-application leads to the same subtantive result. 
estadd local fixed "Yes" , replace
eststo M14 : xtreg Turnout $decades $institutions share_affluent N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M15 : xtreg Turnout $decades $institutions share_affluent N_el_instit i.Vdem_v2elparlel if CV_enforced == 0 & new==0 & EL_TYPE==0 , fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M16 : xtreg Turnout $decades $institutions share_affluent  N_el_instit EL_TYPE if CV_enforced == 0 & new==0 & filter==1 & CV_enforced==0, fe cluster(country) 
estadd local fixed "Yes" , replace
eststo M17 : xtreg Turnout $decades $institutions $new share_affluent N_el_instit EL_TYPE if CV_enforced == 0, fe cluster(country) 
estadd local fixed "Yes" , replace

*LaTeX

esttab  M12 M13 M14 M15 M16 M17  using Table_3.tex, incelldelimite("\:")  one stats(fixed N r2, labels("Country FE" "N" "R2 (within)")  ///
layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 2)) replace b(2) se(2) wide staraux ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) drop(0.Vdem_v2elparlel)  order(decade* EL_TYPE Majority Closeness Concurrent CV  share_affluent  N_el_instit ///
	pre_1974_new post_1974_new postcommunist_new ) nodep /// 
	label varlabels(_cons Constant  /// 
	1.Vdem_v2elparlel "Proportional Elect. System" 2.Vdem_v2elparlel "Mixed Elect. System" 3.Vdem_v2elparlel "Other Elect. System", elist(CV \hline N_el_instit \hline)) /// 
	refcat(share_affluent "\textit{Hypotheses 1 and 2:}", nolabel) ///  
	mtitles("\shortstack{\\M\\Established \\ democracies}"  "\shortstack{\\N\\Established \\ democracies}" ///
	"\shortstack{\\O\\Established \\ democracies}"  ///
	"\shortstack{\\P\\Established dem.\\ leg. elections\\}" ///
	"\shortstack{\\Q\\Established dem.\\ 18 cases (1960-2010)}" "\shortstack{\\R\\ All \\ democracies}") ///
		nonumbers nolegend note("") alignment(d{10})

* Word 

esttab  M12 M13 M14 M15 M16 M17  using Table_3.rtf, one stats(fixed N r2, labels("Country FE" "N" "R2 (within)")  ///
fmt(0 0 2)) incelldelimiter(" ") replace b(2) se(2) wide staraux ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) drop(0.Vdem_v2elparlel)  order(decade* EL_TYPE Majority Closeness Concurrent CV  share_affluent  N_el_instit /// 
	pre_1974_new post_1974_new postcommunist_new ) nodep /// 
	label varlabels(_cons Constant  /// 
	1.Vdem_v2elparlel "Proportional Elect. System" 2.Vdem_v2elparlel "Mixed Elect. System" 3.Vdem_v2elparlel "Other Elect. System", elist(CV \hline N_el_instit \hline)) /// 
	refcat(share_affluent "\textit{Hypotheses 1 and 2:}", nolabel) ///   
	mtitles("M Established   democracies}"  "N Established   democracies" ///
	"O Established   democracies"  ///
	"P Established dem.  leg. elections" ///
	"Q Established dem.  18 cases (1960-2010)}" "\shortstack{ R  All democracies") ///
		nonumbers nolegend note("") 

*****************************************
**********               	   ********** 
**********	Robustness checks  ********** 
**********               	   **********    
*****************************************


*** Table SM1 in the Appendix *** 	

*GDP per capita (log)
eststo clear
eststo MA1: xtreg Turnout $decades $institutions log_gdp_pc share_affluent  N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
*GDP per capita
eststo MA2: xtreg Turnout $decades $institutions c.gdp_pc c.gdp_pc#c.gdp_pc share_affluent N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
*Economic Growth
eststo MA3: xtreg Turnout $decades $institutions  growth_1y growth_2y growth_3y growth_4y share_affluent N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
*Linear trend instead of decade dummies 
eststo MA4 : xtreg Turnout $decades $institutions c.year c.year#c.year share_affluent N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
*Simple pre/post 1960
eststo MA5: xtreg Turnout $decades $institutions share_affluent after_1959_share  N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace
*PCSE with 
eststo MA6: xtpcse Turnout $decades $institutions share_affluent  N_el_instit  EL_TYPE i.country if CV_enforced == 0 & new==0, c(ar1) p 
estadd local fixed "Yes" , replace
*RE
eststo MA7: xtreg Turnout $decades $institutions log_gdp_pc share_affluent N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, re cluster(country) 
estadd local fixed "No" , replace
estadd scalar r2 =e(r2_w) // To use the within R² in the table below
*First-difference 
preserve 
egen decade = group(decade_1940s-decade_2010s) 
collapse Turnout Majority Closeness Concurrent CV share_affluent N_el_instit  EL_TYPE if CV_enforced == 0 & new==0, by(decade country)
xtset country decade 
eststo M8: reg d.Turnout d.EL_TYPE  d.Majority d.Closeness d.Concurrent d.CV d.share d.N_el_instit, cluster(country)
estadd local fixed "No" , replace
restore 


esttab MA1 MA2 MA3 MA4 MA5 MA6 MA7 M8 using Table_SM1.tex, incelldelimite("\:")   one stats(fixed N r2, labels("Country FE" "N" "R2 (within)")  ///
layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") fmt(0 0 2)) replace b(2) se(2) wide staraux ///
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) drop(***.country ) order(decade* year "c.year#c.year" EL_TYPE /// o.* 
	Majority Closeness Concurrent CV share_affluent  N_el_instit)  nodep ///  
	label nolegend nonumbers note("") varlabels(_cons Constant  ///
	c.year#c.year "Year X Year"  c.gdp_pc#c.gdp_pc "GDP p/c X GDP p/c" , elist(CV \hline N_el_instit \hline)) ///
	refcat(share_affluent "\textit{Hypotheses 1 and 2:}", nolabel) ///
	rename(D.EL_TYPE EL_TYPE D.Majority Majority D.Closeness Closeness D.Concurrent Concurrent D.CV CV D.share_affluent share_affluent ///
	D.N_el_instit N_el_instit)  ///
	mtitles("\shortstack{\\A1\\Replication of Model P\\GDP P/C (ln)}" "\shortstack{\\A2\\Replication of Model P\\GDP P/C (quadratic)}" ///
	"\shortstack{\\A3\\Replication of Model P\\GDP Growth}" "\shortstack{\\A4\\Replication of Model P\\ Time Trend (quadratic)}"  ///
	"\shortstack{\\A5\\Replication of Model P\\ Pre/Post 1960 Divide}" "\shortstack{\\A6\\Replication of Model P\\PCSE}" ///
	"\shortstack{\\A7\\Replication of Model P\\ RE \& GDPp/c}" "\shortstack{\\A8\\Replication of Model P\\ FD (Decade-averages)}") ///
	alignment(d{10})

	

*** Table SM2 in the Appendix *** 		

eststo clear
*FDI inflows
eststo clear
eststo M1 : xtreg Turnout $decades $institutions FDI_inflows  EL_TYPE  if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace

*FDI outflows 
eststo M2: xtreg Turnout $decades $institutions FDI_outflows  EL_TYPE  if CV_enforced == 0 & new==0, fe cluster(country) // 
estadd local fixed "Yes" , replace

*Economic polarisation 
eststo M3 : xtreg Turnout $decades $institutions Manif_economic_axis  EL_TYPE  if CV_enforced == 0 & new==0, fe cluster(country) 
estadd local fixed "Yes" , replace

*Cultural polarisation
eststo M4: xtreg Turnout $decades $institutions Manif_cultural_axis  EL_TYPE  if CV_enforced == 0 & new==0, fe cluster(country) // 
estadd local fixed "Yes" , replace

*Robustness checks for simultaneous elections
// Incorporating more detailed data on a subset of countries
merge 1:1 country year  EL_TYPE dup using `"${beg_path}Data\Macro\Election_calendar_data_for_merge.dta"'
drop if _merge == 2
drop _merge
gen non_simult = N_el_instit - (simult_total*(1/6))
replace non_simult = 0 if N_el_instit == 0 // elections with only one level*
lab var non_simult "Non-Simultaneous Elections"
// Supranational Elections
eststo M5: xtreg Turnout $decades $institutions eu  EL_TYPE  if CV_enforced ==0 & new == 0 & share!=. , fe cluster(country) 
estadd local fixed "Yes" , replace
// All non-simultaneous elections
eststo M6: xtreg Turnout $decades $institutions non_simult  EL_TYPE if CV_enforced == 0 & new == 0 & share!=., fe cluster(country) 
estadd local fixed "Yes" , replace
lab var eu "Supranational Elections"

esttab M1 M2 M3 M4 M5 M6 using Table_SM2.tex, incelldelimite("\:")  one stats(fixed N r2, labels("Country FE" "N" "R2 (within)") /// 
layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}")  	 ///
fmt(0 0 2)) replace b(2) se(2) wide staraux star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) order(decade*  /// drop(o.*) 
	Majority Closeness Concurrent CV ) nodep nonumbers /// nobase CV_enforced
	label nolegend note("") substitute("0.00\:(.)" " ")  /// 
	mtitles("\shortstack{\\A9\\Replication of Model K\\FDI Inflows}" "\shortstack{\\A10\\Replication of Model K\\FDI outflows}" ///
	"\shortstack{\\A11\\Replication of Model L\\Economic Polaris.}" /// 
	"\shortstack{\\A12\\Replication of Model L\\Cultural Polaris.}"  ///
	"\shortstack{\\A13\\Replication of Model N\\Supranational Elections}" ///
	"\shortstack{\\A14\\Replication of Model N\\Non-simultaneous elections}") 	alignment(d{10})



	
**********************************************
**********               	   		********** 
**********	Descriptive statistics  ********** 
**********               	   		**********    
********************************************** 

cd `"${beg_path}Results"'

*** Table SM4 in the Appendix *** 		

sutex2 year Turnout $decades $new EL_TYPE $institutions CV_enforced share_affluent N_el_instit El_System* OECD_TU_density_admin MAPP_party_members ///
voting_age18_cum FDI_flows FDI_inflows FDI_outflows Manif_rile Manif_cultural_axis Manif_economic_axis gdp_pc log_gdp_pc growth_1y growth_2y growth_3y growth_4y after non_simult eu, tabular varlab replace saving(Table_SM4.tex) minmax //  title("Summary statistics - Macro Analysis") 


*** Table SM9 in the Appendix *** 

listtex country GDP_12000_start_year  starting_year_18 if el_num ==1 & country<61 using Table_SM9_1.tex, replace missnum(N/A) ///
type rstyle(tabular) head("\begin{tabular}{lll}" `"\textbf{Country}&\textbf{\textdollar12,000 p/c}&\textbf{Vote at 18}\\"') foot("\end{tabular}")  // 
listtex country GDP_12000_start_year starting_year_18 if el_num ==2 & country>60 using Table_SM9_2.tex, replace missnum(N/A) ///
type rstyle(tabular) head("\begin{tabular}{lll}" `"\textbf{Country}&\textbf{\textdollar12,000 p/c}&\textbf{Vote at 18}\\"') foot("\end{tabular}") // Few manual fixes are necessary - Benin, Burkina Faso and Niger have to be set as "missing" on the Vote at 18 variable (to distinguish between a) missing information and b) N/A information due to the fact that the voting age has not yet been reduced to 18)

*** List of Country Elections in the Appendix *** 

listtex country year  EL_TYPE if _n using country_election_list.tex, replace missnum(N/A) ///
type rstyle(tabular)   // 


***********************************
**********               ********** 
**********	Diagnostics  ********** 
**********               **********    
***********************************
	
***** Tests for Model O ***** 
xtreg Turnout $decades $institutions c.share_affluent N_el_instit  EL_TYPE if CV_enforced ==0 & new == 0, fe 
estimates store fixed
xtreg Turnout $decades $institutions c.share_affluent N_el_instit  EL_TYPE if CV_enforced ==0 & new == 0, re  
estimates store random

hausman fixed random, sigmamore //  Prob>chi2 =   0.0002


***** Diagnostics 
tab country, gen(country_temp)
xtserial Turnout $decades $institutions  share_affluent N_el_instit  EL_TYPE country_temp* ///
if CV_enforced ==0 & new == 0 //  Prob > F =      0.0429


******* Tests non-stationarity
xtunitroot fisher Turnout if CV_enforced ==0 & new == 0 , pperron lags(1) // 0.0000 
xtunitroot fisher Turnout if CV_enforced ==0 & new == 0, pperron lags(2) // 0.0000 
xtunitroot fisher Turnout if CV_enforced ==0 & new == 0, pperron lags(3) // 0.0000

***********Tests for contemporaneous correlation 
egen max_el_number = max(el_num), by(country)
tabstat max_el_number, by(country)
xtreg Turnout $decades $institutions share_affluent N_el_instit  EL_TYPE ///
if  CV_enforced ==0 & new == 0 & max_el_num > 20, fe	// (subset for elections with enough overlap)
xtcsd, pesaran  // Pr = 0.1 

***************************************************
**********               				 ********** 
**********	Figure 1 introduction  		 ********** 
**********               				 **********    
***************************************************

foreach var of varlist Turnout {
egen a_1940_`var' = mean(`var') if decade_1940s==1, by(country)
egen a_1950_`var' = mean(`var') if decade_1950s==1, by(country)
egen a_1960_`var' = mean(`var') if decade_1960s==1, by(country)
egen a_1970_`var' = mean(`var') if decade_1970s==1, by(country)
egen a_1980_`var' = mean(`var') if decade_1980s==1, by(country)
egen a_1990_`var' = mean(`var') if decade_1990s==1, by(country)
egen a_2000_`var' = mean(`var') if decade_2000s==1, by(country)
egen a_2010_`var' = mean(`var') if decade_2010s==1, by(country)
}

cap drop temp*  
gen temp1 = decade_1940s ==1 
gen temp2 = decade_2000s ==1 
egen temp3 = max(temp1), by(country)
egen temp4 = max(temp2), by(country)
gen filter_1940_2010 = temp3 ==1 & temp4 ==1 
drop temp*

egen decade = group(decade_2010s decade_2000s decade_1990s decade_1980s decade_1970s decade_1960s decade_1950s decade_1940s), label
label define decade 1"1940s" 2"1950s" 3"1960s"  4"1970s"  5"1980s" 6"1990s"  7"2000s" 8"2010s", replace
label values decade decade

sort country year
bysort country decade: gen temp = _n 

su a_* if temp == 1

gen decade_turnout =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 
replace decade_turnout = r(mean) if temp==1 & decade_`v's == 1
}

gen decade_turnout_f =.
foreach v of numlist 1940 1950 1960 1970 1980 1990 2000 2010 {
su a_`v'_ if temp==1 & decade_`v's == 1 & filter_1940_2010==1
replace decade_turnout_f = r(mean) if temp==1 & decade_`v's == 1 
} 

xtab country // 116 countries
xtab country if filter_1940_2010==1 // 20 countries

set scheme bw
tw connected decade_turnout decade if country==4 || connected decade_turnout_f decade if country==4, ///
xscale(range(.5(.5)8.5)) xlabel(1"1940s" 2"1950s" 3"1960s" 4"1970s" 5"1980s" 6"1990s" 7"2000s" 8"2010s") xtitle("") ytitle("Turnout (%)") ///
legend(lab(1 "All electoral democracies (116 countries)") lab(2 "20 continuous cases (between the 1940s and 2010s)") r(2)) 

graph export "Figure_1.eps", replace // width(3000) 


***************************************************
**********               				 ********** 
**********	Contribution to the decline  ********** 
**********               				 **********    
***************************************************

keep if filter==1
eststo M14 : reg Turnout $decades $institutions share_affluent  N_el_instit  EL_TYPE i.country if CV_enforced ==0 & new==0 & filter==1, cluster(country) 
eststo M14 : reg Turnout $institutions share_affluent  N_el_instit  EL_TYPE i.country if CV_enforced ==0 & new==0 & filter==1, cluster(country) 
preserve 
collapse Turnout $decades $institutions share_affluent N_el_instit  EL_TYPE if share!=. & new==0 & CV_enforced ==0 & filter==1 & decade_1960s==1, by(country)
su Turnout $decades $institutions share_affluent  N_el_instit  EL_TYPE
restore
preserve 
collapse Turnout $decades $institutions share_affluent N_el_instit  EL_TYPE if share!=. & new==0 & CV_enforced ==0 & filter==1 & decade_2010s==1, by(country)
su Turnout $decades $institutions share_affluent  N_el_instit  EL_TYPE
restore

*** Contributions *** 

// Affluent generations 
*Conservative Estimation
di -6.822918*(.4345452-.0041779) // -2.9363608
*Liberal Estimation 
di -10.54139*(.4345452-.0041779) // -4.5366696

// Elective Institutions 
*Conservative Estimation
di -9.371815*(.5925926-.4691358) // -1.1570143
*Liberal Estimation 
di -11.94701*(.5925926-.4691358) // -1.4749396

// Concurrent Elections 
*Conservative Estimation
di 15.09844*(.0833333-.0972222) // -.20970072
*Liberal Estimation 
di 15.62656*(.0833333-.0972222) // -.21703573

*Total Elective Institutions (Elective Institutions + Concurrent)
*Conservative Estimation
di -1.1570143 + -.20970072 // -1.366715
*Liberal Estimation 
di  -1.4749396 + -.21703573 // -1.6919753


// Margin of Victory
*Conservative Estimation
di -.185632*(14.81639-6.701481) // -1.5063868
*Liberal Estimation 
di -.2233724*(14.81639-6.701481) // -1.8126467

// Closeness 
*Conservative Estimation
di -.1509265*(8.681759-15.06657) // .96363718
*Liberal Estimation 
di -.1283842*(8.681759-15.06657) // .81970885

*Total Party Competition (Margin + Closeness)
*Conservative Estimation
di -1.5063868 + .96363718 // -.54274962
*Liberal Estimation 
di  -1.8126467 + .81970885 // -.99293785


*** Figure 4 in the manuscript *** 


clear all
input estim var1 var2 var3 

1 2.9 1.4 0.5
2 4.5 1.7 1

end 

set scheme bw
graph hbar var1-var3 if estim ==1, stack legend(size(small) lab(1 "Generational Replacement (2.9 pp)") lab(3 "Party/Candidate Competition (0.5 pp)")   ///
lab(2 "Elective Institutions (1.4 pp)") row(1))  bar(1, color(gs3)) bar(2, color(gs6)) ///
 bar(3, color(gs9)) bar(4, color(gs12)) /// 
///
ytitle("Contributions to the Decline (8 points in total)", margin(medium)) ylabel(0(1)8, nogrid)  plotregion(margin(0000)) plotregion(margin(none) style(none) color(gs16)) outergap(*0.1) ///
xsize(5) ysize(2) name(graph1, replace) title("Estimation 1 (Conservative)")

graph hbar var1-var3 if estim ==2, stack legend(size(small) lab(1 "Generational Replacement (4.5 pp)") lab(3 "Party/Candidate Competition (1 pp)")   ///
lab(2 "Elective Institutions (1.7 pp)") row(1))  bar(1, color(gs3)) bar(2, color(gs6)) ///
 bar(3, color(gs9)) bar(4, color(gs12)) /// 
///
ytitle("Contributions to the Decline (8 points in total)", margin(medium)) ylabel(0(1)8, nogrid)  plotregion(margin(0000)) plotregion(margin(none) style(none) color(gs16)) outergap(*0.1) ///
xsize(5) ysize(2) name(graph2, replace) title("Estimation 2 (Liberal)")

graph combine graph1 graph2, row(2) col(1) altshrink 

graph export "Figure_4.eps", replace // width(3000) 
	
	

	
	
	


